# -*- coding:utf-8 -*-
# @FileName  :examcoo_comment.py
# @Time      :2023/12/5 
# @Author    :CL
# @email     :1037654919@qq.com
import requests
from bs4 import BeautifulSoup
from retrying import retry
from utils import mongo_manager, get_proxy
from multiprocessing import Pool


examcoo_comment = mongo_manager('examcoo_comment', db='public_data')
examcoo_paper = mongo_manager('examcoo_paper', db='public_data')

# pigcha
proxies = {'http': '127.0.0.1:15732',
           'https': '127.0.0.1:15732'}
headers = {
    "User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:120.0) Gecko/20100101 Firefox/120.0",
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8",
    "Accept-Language": "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2",
    "Accept-Encoding": "gzip, deflate, br",
    # "Connection": "keep-alive",
    "Upgrade-Insecure-Requests": "1",
    "Sec-Fetch-Dest": "document",
    "Sec-Fetch-Mode": "navigate",
    "Sec-Fetch-Site": "none",
    "Sec-Fetch-User": "?1",
    "Pragma": "no-cache",
    "Cache-Control": "no-cache"
}
cookies = {
    "PHPSESSID": "1l3niapfmmh22h1hgtm9npm3h2"
}


@retry(stop_max_attempt_number=5)
def get_comment(data=None):
    if data == None:
        data = {
            "id": "504447695402962945",
            "sdtId": "s1_504447695402962945_1",
            "pid": "1115083",
            "tokenpid": "38a5054bb32994c093e36683b8c0bfe2"
        }
    url = "https://www.examcoo.com/editor/comment/index"
    headers['Referer'] = f'https://www.examcoo.com/editor/do/view/id/{data["pid"]}'
    response = requests.post(url, headers=headers, cookies=cookies, timeout=5,data=data, proxies=get_proxy())
    soups = BeautifulSoup(response.text, 'lxml')
    datas = soups.get_text()
    return datas
def main_export():
    seeds = examcoo_paper.findAll({'status':None})
    for seed in seeds:
        try:
            print(seed['_id'], len(seed['view']))
        except:
            print(seed['_id'], 'error')
            continue
        data = {
            "pid": seed['试卷编号'],
            "tokenpid": seed['vp4tokenpid']
        }
        num = 0
        for vv in seed['view']:
            if 's' in str(vv['id']):  # 试题
                # print(vv['id'], vv['f'])
                num += 1
                sdtId = str(vv["id"]).split('_')[0] + '_' + str(vv["id"]).split('_')[1] + "_" + str(num)
                data['sdtId'] = sdtId
                data['id'] = str(vv["id"]).split('_')[1]
                    # if int(vv['f']) == 1:  # 试题解析
                    # analysis = get_comment(data=data)
                    # data['analysis'] = analysis
                # data['question'] = vv['a']
                # data['choice'] = vv['b']
                # data['answer'] = vv['c']
                # data['socre'] = vv['d']
                # data['have_comment'] = vv['e']
                # data['have_analysis'] = vv['f']
                data.update(vv)
                data["_id"] = vv['id']
                try:
                    examcoo_comment.insertOne(data)
                except  Exception as e:
                    examcoo_comment.updateOne({"_id":data["_id"]},data)

                    # print(e)
        seed['status'] = 'success'
        examcoo_paper.updateOne({'_id': seed['_id']}, seed)
def main():
    while True:
        seeds = examcoo_comment.findAll({'f':1,'analysis':None}).limit(1000)

        work_list = []
        for seed in seeds:
            work_list.append(seed)
        print(len(work_list))
        pool = Pool(processes=10)
        pool.map(get_info, work_list)
        pool.close()  # 关闭进程池，不再接受新的进程
        pool.join()
        if len(work_list)<1000:
            break
def get_info(seed):
    data = {
        "pid": seed['pid'],
        "tokenpid": seed['tokenpid'],
        'sdtId': seed['sdtId'],
        'id': str(seed['id']).split("_")[1]
    }
    print(data)
    try:
        analysis = get_comment(data=data)
        seed['analysis'] = analysis
    except:
        pass
    examcoo_comment.updateOne({"_id":seed["_id"]},seed)
def data_export_20231214():
    seeds = examcoo_paper.findAll({'status': 'success'})
    for seed in seeds:
        try:
            print(seed['_id'], len(seed['view']))
        except:
            print(seed['_id'], 'error')
            continue

        for vv in seed['view']:
            data = {
                "pid": seed['试卷编号'],
                "tokenpid": seed['vp4tokenpid']
            }
            if 's' in str(vv['id']):  # 试题
                data.update(vv)
                data["_id"] = vv['id']
                examcoo_comment.updateOne({"_id": data["_id"]}, data)

                    # print(e)
        seed['status'] = '1'
        examcoo_paper.updateOne({'_id': seed['_id']}, seed)



if __name__ == "__main__":
    print('beidi')
    # get_comment()
    # get_vp4tokenpid()
    # get_view()
    # main()
    # main_export()
    # examcoo_comment.updateMany({}, { 'b': None})
    data_export_20231214()
    examcoo_comment.close()
    examcoo_paper.close()
